package com.liujie.controller;

import com.liujie.common.R;
import com.liujie.pojo.Room;
import com.liujie.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
@CrossOrigin(origins = "*",maxAge = 3600)//解决跨域
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 查询所有房间信息
     * @return 所有房间信息
     */
    @GetMapping
    public R getAll(){
        List<Room> rooms = roomService.findAll();
        return R.success(rooms);
    }

    /**
     * 通过房间号查找
     * @param id 房间号
     * @return 返回房间信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable String id){
        Room room = roomService.getById(id);
        if (room!=null){
            return R.success(room);
        }
        return R.error("房间不存在！");
    }

    /**
     * 创建房间
     * @param room 房间实体
     * @return 结果
     */
    @PostMapping
    public R save(@RequestBody Room room){
        boolean save = roomService.save(room);
        if (save){
            return R.success(room);
        }
        return R.error("房间创建失败！");
    }

    /**
     * 更新房间信息
     * @param room 需要更新的参数
     * @return 结果
     */
    @PutMapping
    public R update(@RequestBody Room room){
        boolean b = roomService.updateById(room);
        if (b){
            return R.success("房间更新成功！");
        }
        return R.error("房间更新失败！");
    }

    /**
     * 删除房间
     * @param room 房间实体
     * @return 结果
     */
    @DeleteMapping
    public R delete(@RequestBody Room room){
        room.setDeleted(1);
        boolean b = roomService.updateById(room);
        if (b){
            return R.success("房间删除成功！");
        }
        return R.error("房间删除失败！");
    }
}
